package com.fingard.cluster;

import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

public class HostInfo {
	/**主机服务地址*/
	public String addr;
	
	/**公钥*/
	public String publicKey;
	
	/**最大连接数*/
	public int maxLinks;
	
	public String name;
	
	public String ip;
	public int port;
	
	public boolean enable;
	
	public HostInfo(XElement pXe) throws Exception{
		loadFromXe(pXe);
	}
	
	private void loadFromXe(XElement pXe) throws Exception{
		enable = pXe.getAttrValueAsBool("enable");
		addr = pXe.getSingleTextNotNull("addr");
		publicKey = pXe.getSingleTextNotNull("publicKey");
		String max = pXe.getSingleTextNotNull("maxLinks");
		if(StringHelper.hasAnyChar(max)){
			maxLinks = Integer.parseInt(max);
			if(maxLinks <= 0){
				maxLinks = 1;
			}
		}else{
			maxLinks = 1;
		}
		String[] ipPort = addr.split(":");
		ip = ipPort[0].trim();
		if(ipPort.length==2){
			port = Integer.parseInt(ipPort[1]);
		}else{
			throw new Exception("主机服务地址格式有误："+addr);
		}
	}
}
